package com.zym.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zym.entity.SysUser;
import com.zym.exception.MyException;
import com.zym.service.SysUserService;
import com.zym.utils.EasyExcel.ExcelUtil;
import com.zym.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Api(tags = "test")
@RestController
@RequestMapping("/sysuser")
public class SysUserController {


    @Autowired
    private SysUserService sysUserService;
    @GetMapping("/test")
    @ApiOperation("测试")
    public R test(){
        Page<SysUser> page=new Page<>(2,1);
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name","王");
        Page<SysUser> page1 = sysUserService.page(page, queryWrapper);
        Map<String,Object> map=new HashMap<>();
        map.put("data",page1.getRecords());
        map.put("total",page1.getTotal());
        return R.okHasData(map);
    }

    @ApiOperation("数据导出")
    @GetMapping("/export")
    public void exportData(HttpServletResponse response){
        List<SysUser> list = sysUserService.list(null);
        String fileName = UUID.randomUUID().toString().replaceAll("-", "");

        try {
            ExcelUtil.writeExcel(response,"用户数据",SysUser.class,list);
        } catch (Exception e) {
           throw new MyException("导出出错！");
        }
    }
    @ApiOperation("数据导入")
    @PostMapping("/import")
    public void importData(MultipartFile file) throws IOException {


    }
}
